# -*- coding:utf-8 -*-
'''
@Author: lindes
'''
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from .models import PraiseArticle, InformArticle, CollectArticle, CommentArticle


@receiver(post_save, sender=PraiseArticle)
def create_pa(sender, instance=None, created=False, **kwargs):
    if created:
        article = instance.article
        article.praise_count += 1
        article.save()


@receiver(post_delete, sender=PraiseArticle)
def delete_pa(sender, instance=None, created=False, **kwargs):
    article = instance.article
    article.praise_count -= 1
    article.save()


@receiver(post_save, sender=InformArticle)
def create_ia(sender, instance=None, created=False, **kwargs):
    if created:
        article = instance.article
        article.inform_count += 1
        article.save()


@receiver(post_delete, sender=InformArticle)
def delete_ia(sender, instance=None, created=False, **kwargs):
    article = instance.article
    article.inform_count -= 1
    article.save()


@receiver(post_save, sender=CollectArticle)
def create_ca(sender, instance=None, created=False, **kwargs):
    if created:
        article = instance.article
        article.collect_count += 1
        article.save()


@receiver(post_delete, sender=CollectArticle)
def delete_ca(sender, instance=None, created=False, **kwargs):
    article = instance.article
    article.collect_count -= 1
    article.save()


@receiver(post_save, sender=CommentArticle)
def create_cma(sender, instance=None, created=False, **kwargs):
    if created:
        article = instance.article
        article.comment_count += 1
        article.save()


@receiver(post_delete, sender=CommentArticle)
def delete_cma(sender, instance=None, created=False, **kwargs):
    article = instance.article
    article.comment_count -= 1
    article.save()
